﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Leo.Entities;
using Leo.BL;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;


namespace VeterinariaDrLeo.Controllers
{
    public class CategoriaController : Controller
    {
        //
        // GET: /Categoria/

        [Dependency]

        public ICategoriaBL categoriaBL { get; set; }

        public ActionResult Index()
        {
            var listarCategoria = categoriaBL.GetFromCategoriasByCriterio("");
            return View(listarCategoria);
        }

        [HttpPost]

        public ActionResult Index(string criterio = "")
        {
            var listarCategoria = categoriaBL.GetFromCategoriasByCriterio(criterio);
            return View(listarCategoria);
        }

        public ActionResult Create()
        {
            var categoria = categoriaBL.GetFromCategoriasByCriterio("");
            return View();
        }

        [HttpPost]
        public ActionResult Create(Categoria categoria)
        {
            try
            {
                categoriaBL.AddCategoria(categoria);
                return RedirectToAction("index");
            }
            catch
            {
                return View();
            }

        }

        public ActionResult Edit(Int32 id)
        {
            var categoria = categoriaBL.GetFromCateriaByID(id);
            return View(categoria);
        }

        [HttpPost]
        public ActionResult Edit(int id, Categoria categoria)
        {
            try
            {
                categoriaBL.UpdateCategoria(categoria);
                return RedirectToAction("index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            categoriaBL.RemoveCategoria(id);
            return RedirectToAction("index");
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("index");
            }

            catch
            {
                return View();
            }

        }

    }
}
